/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.report;

import easybooking.data.Airline;
import easybooking.data.Customer;
import easybooking.data.Flight;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.TreePath;

/**
 *
 * @author Tim Schröder
 */
public class ReportClientController implements ActionListener, TreeSelectionListener {

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                rcc = new ReportClientController();
                rcg = new ReportClientGui();
                rcg.createAndShowGUI(rcc);
            }
        });
    }
    private static ReportClientGui rcg;
    private static ReportClientController rcc;
    private RCInterfaceClient rcic = new RCInterfaceClient();
    private RCConfigDialog rccd = new RCConfigDialog(this);
    private RCFlightForm ff = new RCFlightForm(this);
    private RCCustomerForm cf = new RCCustomerForm(this);
    private RCReportDialog rd = new RCReportDialog(this);
    private JFileChooser fc = new JFileChooser();
    private RCCustomerReportDialog crd = new RCCustomerReportDialog(this);
    private RCCustomerReportDialogAirlineSelect crdas = new RCCustomerReportDialogAirlineSelect(this);

    public void setServer() {
        rccd.createAndShowDialog();
    }

    public RCInterfaceClient getInterfaceClient() {
        return rcic;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("connectToServer")) {
            if (!rcic.initServerConnection(rccd.getHostname())) {
                JOptionPane.showMessageDialog(null, "Server connection failed");
            } else {
                rccd.dispose();
            }
        }
        if (e.getActionCommand().equals("createFlightSeatingReport")) {
            if (ff.getSelectedFlight() == null) {
                JOptionPane.showMessageDialog(null, "No flight selected");
            } else {
                Flight f = (Flight) ff.getSelectedFlight();
                String report = rcic.createFlightSeatingHtmlReport(f);
                rd.createAndShowDialog(report);
            }

        }


        if (e.getActionCommand().equals("createCustomerReport")) {
            if (cf.getSelectedCustomer() == null) {
                JOptionPane.showMessageDialog(null, "No customer selected");
            } else {
                Customer c = (Customer) cf.getSelectedCustomer();
                // pases the customer which should be edited
                crd.setCustomer(c);
                crd.createAndShowDialog(c);
            }
        }

        if (e.getActionCommand().equals("CustomerReportDialogAirlineSelect")) {
            crdas.setTableData(rcc.getInterfaceClient().getAllAirlines());
            crdas.createAndShowDialog();
        }

        if (e.getActionCommand().equals("CustomerReportSelectAirline")) {
            crd.setAirline((Airline) crdas.getSelectedAirline());
            crdas.dispose();
        }


        if (e.getActionCommand().equals("CustomerReportDialogAirlineAll")) {
            crd.setAirline(null);

        }
        if (e.getActionCommand().equals("CreateCustomerReportDialogOk")) {

            if (crd.getAirline() == null) {
                String report = rcic.createAllFlightsOfCustomerHtmlReport(crd.getCustomer());
                rd.createAndShowDialog(report);
                crd.dispose();
            } else {
                String report = rcic.createAllFlightsOfCustomerWithArilineHtmlReport(crd.getCustomer(), crd.getAirline());
                rd.createAndShowDialog(report);
                crd.dispose();
            }
        }

        if (e.getActionCommand().equals("SaveReport")) {
            JButton source = (JButton) e.getSource();
            //File reportFile = new File(fsrd.getReport());

            fc.setFileFilter(new FileNameExtensionFilter("HTML file", "htm", "html"));
            fc.updateUI();

            int result = fc.showSaveDialog(source.getParent());

            File file = new File(rd.getReport());


            if (result == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                if (!file.getAbsolutePath().endsWith(".html")) {
                    file = new File(file.getAbsolutePath() + ".html");
                }
                try {
                    BufferedWriter out = new BufferedWriter(new FileWriter(file));
                    out.write(rd.getReport());
                    out.close();
                } catch (IOException ex) {
                    Logger.getLogger(ReportClientController.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }


    }

    public void valueChanged(TreeSelectionEvent e) {
        TreePath path = e.getNewLeadSelectionPath();

        Object a = path.getLastPathComponent().toString();

        if (a == "EasyBooking") {
            rcg.setRightPanel(rcg.getMainPanel());
        }

        if (a == "Flight Seating") {

            // removes all old components
            ff.removeAll();
            // set table date
            ff.setTableData(rcic.getAllFlights());
            // prepare form for displaying
            ff.createAndShowForm();
            // show cf in the right panel
            rcg.setRightPanel(ff);
        }

        if (a == "Customer Report") {

            // removes all old components
            cf.removeAll();
            // set table date
            cf.setTableData(rcic.getAllCustomers());
            // prepare form for displaying
            cf.createAndShowForm();
            // show cf in the right panel
            rcg.setRightPanel(cf);
        }
    }
}
